package com.example.animations.activities;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.drawable.DrawableCompat;

import com.example.animations.R;
import com.example.animations.controls.SliderView;
import com.skydoves.colorpickerview.ColorEnvelope;
import com.skydoves.colorpickerview.ColorPickerDialog;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;

public class ColorAnimationActivity extends AppCompatActivity {
    private final float minDuration = 0.1f;
    private final float maxDuration = 2.0f;
    private int _firstColor = Color.parseColor("#FF000F");
    private final ColorDrawable _firstColorDrawable = new ColorDrawable(_firstColor);
    private int _secondColor = Color.parseColor("#FFF500");
    private final ColorDrawable _secondColorDrawable = new ColorDrawable(_secondColor);
    private View _firstColorHintView;
    private View _secondColorHintView;
    private float _duration = 1.0f;
    private TransitionDrawable _transitionDrawable;
    private boolean _isSecondColor = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_animation);

        _firstColorHintView = findViewById(R.id.first_color_hint_view);
        updateColorSelection(true, _firstColor);
        _secondColorHintView = findViewById(R.id.second_color_hint_view);
        updateColorSelection(false, _secondColor);

        final AlertDialog firstDialog = buildColorPickerDialog(true).create();
        final AlertDialog secondDialog = buildColorPickerDialog(false).create();

        ((LinearLayout) findViewById(R.id.first_color_layout)).setOnClickListener(view -> firstDialog.show());
        ((LinearLayout) findViewById(R.id.second_color_layout)).setOnClickListener(view -> secondDialog.show());

        ((Button) findViewById(R.id.play_button)).setOnClickListener(view -> startAnimation());

        SliderView durationSlider = findViewById(R.id.duration_slider);
        durationSlider.setTitle("Duration");
        durationSlider.setMinMaxValues(minDuration, maxDuration);
        durationSlider.setValue(_duration);
        durationSlider.onValueChanged(this::onDurationChanged);

        Drawable[] colors = new Drawable[]{_firstColorDrawable, _secondColorDrawable};

        _transitionDrawable = new TransitionDrawable(colors);
        findViewById(R.id.animatable_layout).setBackground(_transitionDrawable);
    }

    private ColorPickerDialog.Builder buildColorPickerDialog(boolean isFirst) {
        return new ColorPickerDialog.Builder(this)
                .setTitle(isFirst ? "First Color" : "Second Color")
                .setPositiveButton("Confirm",
                        new ColorEnvelopeListener() {
                            @Override
                            public void onColorSelected(ColorEnvelope envelope, boolean fromUser) {
                                String hex = "#" + envelope.getHexCode();
                                int color = Color.parseColor(hex);
                                updateColorSelection(isFirst, color);
                            }
                        })
                .setNegativeButton("Cancel", (d, i) -> d.dismiss())
                .attachAlphaSlideBar(false)
                .attachBrightnessSlideBar(false);
    }

    private void updateColorSelection(boolean isFirst, int color) {
        if (isFirst) {
            _firstColor = color;
            Drawable backgroundDrawable = DrawableCompat.wrap(_firstColorHintView.getBackground());
            DrawableCompat.setTint(backgroundDrawable, color);
            _firstColorHintView.setBackground(backgroundDrawable);

            _firstColorDrawable.setColor(color);
            if (_transitionDrawable != null) {
                _transitionDrawable.setDrawable(0, _firstColorDrawable);
            }
        } else {
            _secondColor = color;
            Drawable backgroundDrawable = DrawableCompat.wrap(_secondColorHintView.getBackground());
            DrawableCompat.setTint(backgroundDrawable, color);
            _secondColorHintView.setBackground(backgroundDrawable);

            _secondColorDrawable.setColor(color);
            if (_transitionDrawable != null) {
                _transitionDrawable.setDrawable(1, _secondColorDrawable);
            }
        }
    }

    private void onDurationChanged(float value) {
        _duration = value;
    }

    private void startAnimation() {
        int duration = Math.round(_duration * 1000);

        if (_isSecondColor) {
            _transitionDrawable.reverseTransition(duration);
            _isSecondColor = false;
        } else {
            _transitionDrawable.startTransition(duration);
            _isSecondColor = true;
        }
    }
}
